package com.candy.test;

import com.candy.dao.AccountDao;
import com.candy.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Client {
    @Test
    public void test01(){
//        加载容器
//        ClassPathXmlApplicationContext：可以加载类路径下的配置文件
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//       调用getBean方法进行对象创建
        AccountService accountService=(AccountService) ac.getBean("accountService");
        System.out.println(accountService);
        accountService.save();
        System.out.println("==============================================");
        AccountDao accountDao= (AccountDao)ac.getBean("accountDao");
        System.out.println(accountDao);
    }
    @Test
    public void test02(){
//        加载容器
//        FileSystemXmlApplicationContext可以加载磁盘上的配置文件，（要求有访问权限）
        ApplicationContext ac=new FileSystemXmlApplicationContext("E:\\授课材料\\文档材料\\java\\ssm\\Spring\\beans.xml");
//       调用getBean方法进行对象创建
        AccountService accountService=(AccountService) ac.getBean("accountService");
        System.out.println(accountService);
        accountService.save();
        System.out.println("==============================================");
        AccountDao accountDao= (AccountDao)ac.getBean("accountDao");
        System.out.println(accountDao);
    }
    @Test
    public void test03(){
//        加载容器
//        ClassPathXmlApplicationContext：可以加载类路径下的配置文件
        ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//       调用getBean方法进行对象创建
        AccountService accountService=(AccountService) ac.getBean("accountService");
        AccountService accountService2=(AccountService) ac.getBean("accountService");
        System.out.println(accountService);
        System.out.println(accountService2);
        System.out.println(accountService==accountService2);
//        关闭容器
        ac.close();

    }

    @Test
    public void test04(){
//        加载容器
//        ClassPathXmlApplicationContext：可以加载类路径下的配置文件
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//       调用getBean方法进行对象创建
//        使用静态工厂方法创建bean对象的
        AccountService accountService2=(AccountService) ac.getBean("accountService2");
        System.out.println("静态工厂："+accountService2);
//        使用工厂方法进行创建bean对象的
        AccountService accountService3=(AccountService) ac.getBean("accountService3");
        System.out.println("工厂方法"+accountService3);
    }


}
